
#pragma once

#include <pthread.h>

namespace dulymoo {

class Thread
{
public:
    Thread(void (*func)(void*), void* arg, unsigned long stack_size=0);
    ~Thread();

private:
    Thread(const Thread&); // unimplemented
    Thread& operator= (const Thread&); // unimplemented

public:
    bool Start();
    bool Join();

    static void* GetSpecific();
    static bool  SetSpecific(void*);

private:
    static void* ThreadProc(void*);
    static void  InitKey();

private:
    void (* const func_)(void*);
    void* const arg_;
    const unsigned long stack_size_;
    pthread_t thread_;

    static pthread_key_t key_;
    static bool key_initialized_;
    static pthread_once_t key_once_;
};

} // namespace dulymoo

